package com.woniu.woniu_bx.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniu.woniu_bx.pojo.Order;
import com.woniu.woniu_bx.sqlprovider.OrderSqlProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;


/**
 * @ClassName OrderMapper
 * @Description 订单 Mapper接口
 * @Author 魏才林
 * @Date 2022/8/27 16:27
 * @Version 1.0
 **/

public interface OrderMapper extends BaseMapper<Order> {

    @Select("select count(1) from bx_order where buy_time like concat('%', #{curDate}, '%')")
    Integer getNumsOfAllOrderByCurDate(String curDate);

//    @Update("update bx_order set status='n' where id=#{id}")
//    Integer delete(Integer id);

    /**
     * 获取所有等待操作的订单
     *
     * @return
     */
    @Select("select * from bx_order where state ='1'")
    List<Order> getWaitSureOrder();

    /**
     * 获取所有等待操作的订单
     *
     * @return
     */
    @Select("select * from bx_order where state = '3'")
    List<Order> getWaitFinishOrder();

    /**
     * 条件查询
     *
     * @param order
     * @return
     */
    @Results(
            {
                    @Result(column = "oid", property = "id", id = true),
                    @Result(column = "buy_time", property = "buyTime"),
                    @Result(column = "goods_id", property = "goodsId"),
                    @Result(column = "order_num", property = "orderNum"),
                    @Result(column = "num", property = "num"),
                    @Result(column = "om", property = "money"),
                    @Result(column = "state", property = "state"),
                    @Result(column = "pw_option", property = "pwOption"),
                    @Result(column = "account", property = "user.account")
            }
    )
    @SelectProvider(type = OrderSqlProvider.class, method = "getByCondition")
    List<Order> getByCondition(Order order);

    /**
     * 查询所有的订单
     *
     * @return
     */
    @Results(
            {
                    @Result(column = "oid", property = "id", id = true),
                    @Result(column = "buy_time", property = "buyTime"),
                    @Result(column = "goods_id", property = "goodsId"),
                    @Result(column = "order_num", property = "orderNum"),
                    @Result(column = "num", property = "num"),
                    @Result(column = "money", property = "money"),
                    @Result(column = "state", property = "state"),
                    @Result(column = "pw_option", property = "pwOption"),
                    @Result(column = "user_id", property = "user", one = @One(select = "com.woniu.woniu_bx.mapper.UserMapper.selectById", fetchType = FetchType.LAZY))
            }
    )
    @Select("select * from bx_order")
    List<Order> getAll();

    /**
     * 根据类别查询订单
     *
     * @return
     */
    @Select("select category_id,count(*) AS orderNumber from bx_order o,bx_goods g group by category_id")
    @Results(
            {
                    @Result(column = "orderNumber", property = "orderNumber"),
                    @Result(column = "category_id", property = "categoryId"),
            }
    )
    List<Order> getOrderNumberGroupByCategoryId();

    /**
     * 根据id查询订单
     *
     * @param id
     * @return
     */
    @Select("select * from bx_order where id = #{id}")
    @Results(
            {
                    @Result(column = "oid", property = "id", id = true),
                    @Result(column = "buy_time", property = "buyTime"),
                    @Result(column = "goods_id", property = "goodsId"),
                    @Result(column = "order_num", property = "orderNum"),
                    @Result(column = "num", property = "num"),
                    @Result(column = "money", property = "money"),
                    @Result(column = "state", property = "state"),
                    @Result(column = "pw_option", property = "pwOption"),
                    @Result(column = "user_id", property = "user", one = @One(select = "com.woniu.woniu_bx.mapper.UserMapper.selectById", fetchType = FetchType.LAZY))
            }
    )
    Order getById(int id);

    /**
     * 根据用户id查询订单列表
     * 前台专用
     *
     * @param userId
     * @return
     */
    @Select("select * from bx_order where user_id = #{userId} and state !='n'")
    List<Order> getByUserId(int userId);

    /**
     * 根据用户id查询订单列表(陪玩)
     * 前台专用
     *
     * @param userId
     * @return
     */
    @Select("SELECT o.* FROM bx_order o,bx_user u WHERE o.user_id = u.id and o.user_id = #{userId} AND o.state !='n' AND u.auth_pw='y'")
    List<Order> getByPwUserId(@Param("userId") Integer userId);

    /**
     * 改变订单状态为n
     * 前台专用
     *
     * @param id
     */
    @Update("update bx_order set state='n' where id = #{id}")
    void delete(int id);

    /**
     * 改变订单状态为5
     * 前台专用
     *
     * @param id
     */
    @Update("update bx_order set state='5' where id = #{id}")
    void confirm(int id);

    /**
     * 改变订单状态为3
     * 前台专用
     *
     * @param id
     */
    @Update("update bx_order set state='3' where id = #{id}")
    void served(int id);

    /**
     * 改变订单状态为2（进行中）
     * 前台专用
     *
     * @param id
     */
    @Update("update bx_order set state='2' where id = #{id}")
    void accept(int id);

    /**
     * 改变订单状态为4（已取消）
     * 前台专用
     *
     * @param id
     */
    @Update("update bx_order set state='4' where id = #{id}")
    void refuse(int id);

    /**
     * @return
     * @Description 根据陪玩id查出其对应订单状态或所有状态的所有订单(根据时间排序)
     * @Date 12:31 2022/9/2
     * @Author 魏才林
     * @Param
     **/
    @Select({"<script>" +
            "select bo.*,bc.name category_name,bc.img category_img,bu.avr pwAvr " +
            "from bx_order bo,bx_user bu,bx_goods bg ,bx_category bc " +
            "where bu.id=bg.user_id and bg.id=bo.goods_id and bg.category_id = bc.id " +
            "and bu.id = #{pwId} and bo.state in " +
            "<foreach collection='status' item='s'  open='('  separator=','  close=')'>" +
            "#{s}" +
            "</foreach>" + "order by bo.buy_time desc" +
            "</script>"})
    List<Order> getOrdersByPwIdAndStatus(@Param("pwId") Integer pwId, @Param("status") List<String> status);


    /**
     * @return
     * @Description 根据陪玩id查出其对应订单状态或所有状态的所有订单(根据时间排序)
     * @Date 12:31 2022/9/2
     * @Author 魏才林
     * @Param
     **/
    @Select({"<script>" +
            "select bo.*,bc.name category_name,bc.img category_img,bu.avr pwAvr " +
            "from bx_order bo,bx_user bu,bx_goods bg ,bx_category bc " +
            "where bo.user_id = bu.id and bo.goods_id=bg.id and bg.category_id = bc.id " +
            "and bu.id = #{userId} and bo.state in " +
            "<foreach collection='status' item='s'  open='('  separator=','  close=')'>" +
            "#{s}" +
            "</foreach>" + "order by bo.buy_time desc" +
            "</script>"})
    List<Order> getOrdersByUserIdAndStatus(@Param("userId") Integer userId, @Param("status") List<String> status);

    /**
     * @return
     * @Description 根据订单id查出其对应订单状态或所有状态的所有订单(根据时间排序)
     * @Author 魏才林
     * @Param
     **/
    @Select("select bo.*,bc.name category_name,bc.img category_img,bu.avr pwAvr, bu.nick pwNick,bg.description goodsDescription " +
            "from bx_order bo,bx_user bu,bx_goods bg ,bx_category bc " +
            "where bu.id=bg.user_id and bo.goods_id=bg.id and bg.category_id = bc.id " +
            "and bo.id = #{id} ")
    Order getOrderDetailById(Integer id);

}
